package com.example.user.service;



import com.example.user.dao.ProductimageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.PageParam;
import entity.Rsp;
import entity.Productimage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ProductimageServiceImpl implements ProductimageService {

    @Autowired
    ProductimageMapper productimageMapper;

    @Override
    public PageInfo<Productimage> searchSomeByPage(PageParam<Productimage> pageParam) {

        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Productimage> productimageList=productimageMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Productimage> productimagePageInfo = new PageInfo<Productimage>(productimageList);

            return productimagePageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Productimage> productimageList=productimageMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Productimage> productimagePageInfo = new PageInfo<Productimage>(productimageList);

            return productimagePageInfo;

        }

    }

    @Override
    public Productimage getOneById(String id) {
        return productimageMapper.getOneById(id);
    }

    @Override
    public Rsp addOne(Productimage productimage) {

        productimage.setId(UUID.randomUUID().toString().replaceAll("-", ""));
//        productimage.setCreateTime(new Date());



        return productimageMapper.addOne(productimage)==1?Rsp.success("添加成功"):Rsp.fail().msg("添加失败");
    }

    @Override
    public Rsp deleteOneTrue(String id) {



        return productimageMapper.deleteOneTrue(id)==1 ? Rsp.success(null).msg("物理删除成功"):Rsp.fail().msg("物理删除失败");
    }

    @Override
    public Rsp update(Productimage productimage) {


        return productimageMapper.update(productimage)==1?Rsp.success(null).msg("修改成功"):Rsp.fail().msg("修改失败");
    }

    @Override
    public Rsp deleteLogical(Productimage productimage) {

        productimage.setIsDeleted(1);

        return productimageMapper.update(productimage)==1?Rsp.success(null).msg("逻辑删除成功"):Rsp.fail().msg("逻辑删除失败");
    }

    @Override
    public Rsp recoverLogical(Productimage productimage) {

        productimage.setIsDeleted(0);

        return productimageMapper.update(productimage)==1?Rsp.success(null).msg("逻辑恢复成功"):Rsp.fail().msg("逻辑恢复失败");
    }

    @Override
    public Rsp deleteBatchTrue(List<String> ids) {


        return productimageMapper.deleteBatchTrue(ids)==ids.size()?Rsp.success(null).msg("批量物理删除成功"):Rsp.fail().msg("批量物理删除失败");
    }

    @Override
    public Rsp deleteBatchLogical(List<String> ids) {


        return productimageMapper.deleteBatchLogical(ids)==ids.size()?Rsp.success(null).msg("批量物理删除成功"):Rsp.fail().msg("批量物理删除失败");
    }


    @Override
    public Rsp deleteSomeLogicalByCondition(Productimage productimage) {


        return productimageMapper.deleteSomeLogicalByCondition(productimage)>0?Rsp.success(null).msg("条件逻辑物理删除成功"):Rsp.fail().msg("条件逻辑物理删除失败");
    }

    @Override
    public Rsp deleteSomeTrueByCondition(Productimage productimage) {


        return productimageMapper.deleteSomeTrueByCondition(productimage)>0?Rsp.success(null).msg("条件逻辑物理删除成功"):Rsp.fail().msg("条件逻辑物理删除失败");
    }


}
